package com.woniuxy.lab.woniu_lab.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lab.woniu_lab.commons.Result;

import com.woniuxy.lab.woniu_lab.service.MarkService;
import com.woniuxy.lab.woniu_lab.service.dto.markDto.UserMarkDto;
import com.woniuxy.lab.woniu_lab.util.JwtUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.*;


/**
 * <p>
 * 用户收藏表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-03-15
 */
@RestController
@RequestMapping("/mark")
@Api(tags = "[F]用户收藏相关接口")
@Slf4j
@Validated
public class MarkController {

    @Resource
    private MarkService markService;

    @ApiOperation("20 查询我收藏的安全实验")
    @GetMapping("/queryMyExper")
    public Result<IPage<UserMarkDto>> queryMyMark(
            @ApiParam(value = "第几页") @Min(value = 1,message = "当前页最小为1") @NotNull(message = "当前页不能为空") Integer page,
            @ApiParam(value = "每页条数") @NotNull(message = "展示数量不能为空") @Max(value = 50,message = "展示数量最大为50")
            @Min(value = 1,message = "展示数量最小为1") Integer size
    ){
        log.info("查询我收藏的实验页:{},页大小:{}",page,size);
        IPage<UserMarkDto> iPage = new Page<>(page == null ? 1 : page, size == null ? 10 : size);
        IPage<UserMarkDto> userMarkDto = markService.queryMyMark(iPage);
        return Result.success(200,"查询成功",userMarkDto);
    }


    @ApiOperation("21 查询我收藏的CTF实验")
    @GetMapping("/queryMyCTF")
    public Result<IPage<UserMarkDto>> queryMyCTF(
            @ApiParam(value = "第几页") @Min(value = 1,message = "当前页最小为1") @NotNull(message = "当前页不能为空") Integer page,
            @ApiParam(value = "每页条数") @NotNull(message = "展示数量不能为空") @Max(value = 50,message = "展示数量最大为50")
            @Min(value = 1,message = "展示数量最小为1") Integer size
    ){
        log.info("查询收藏的CTF实验:{},页大小:{}",page,size);
        IPage<UserMarkDto> iPage = new Page<>(page == null ? 1 : page, size == null ? 10 : size);
        IPage<UserMarkDto> userMarkDto = markService.queryMyCTF(iPage);
        return Result.success(200,"查询成功",userMarkDto);
    }


    @ApiOperation("56 收藏实验")
    @PostMapping("collectRange")
    public Result<Object> collect(@ApiParam(value = "靶场Id") @RequestParam @Min(value = 1,message = "靶场ID最小为1")
                                      @NotNull(message = "靶场ID不能为空") Integer rangeId){
        log.info("收藏靶场{}",rangeId);
        Integer userId = JwtUtil.getUserIdFromReq();
        markService.collectRange(userId,rangeId);
        return Result.success(200,"收藏成功",null);

    }

    @ApiOperation("23 删除收藏的实验")
    @DeleteMapping("delCollectRange")
    public Result<Object> delCollectRange(@ApiParam(value = "靶场Id") @RequestParam("rangeId")
                                              @NotNull(message = "靶场ID不能为空") @Min(value = 1,message = "靶场ID最小为1") Integer rangeId){
        log.info("删除收藏靶场{}",rangeId);
        markService.delCollectRange(rangeId);
        return Result.success(200,"删除成功",null);
    }

}

